package com.py.custom.business.demo.feign.gateway;

import com.py.custom.business.demo.feign.adapter.ChannelAdapter;
import com.py.custom.business.demo.feign.enums.ChannelType;
import com.py.custom.business.demo.protocol.dto.MessageTaskDTO;
import com.py.custom.business.demo.protocol.dto.SendResultDTO;
import com.py.custom.common.web.exception.BizException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * description：消息发送类；所有调用第三的接口，都以Gateway结尾
 * feign模块：所有依赖此模块的项目；都只能使用gateway包下类里面的方法
 */
@Component
public class MessageSendGateway {

    @Resource
    private List<ChannelAdapter> channelAdapterList;

    public ChannelAdapter getChannel(Integer channelId) {
        String clazz = ChannelType.getClazz(channelId);
        for (ChannelAdapter channelAdapter : channelAdapterList) {
            if (channelAdapter.supports(clazz)) {
                return channelAdapter;
            }
        }
        throw new BizException("找不到消息发送适配类，请检查渠道");
    }

    public SendResultDTO send(MessageTaskDTO messageTaskDto) {
        ChannelAdapter adapter = getChannel(messageTaskDto.getChannelId());
        return adapter.sendMessage(messageTaskDto);
    }

}
